cmake_minimum_required(VERSION 3.0.2)
project(pc_udp_streamer)

# 编译选项
add_compile_options(-std=c++14 -O3 -Wall -Wextra -pthread)

# ------------------------
# Catkin / 依赖
# ------------------------
find_package(catkin REQUIRED COMPONENTS
  roscpp
  sensor_msgs
  pcl_ros
  pcl_conversions
  message_filters
  tf2_ros
  tf2_eigen
  dynamic_reconfigure
)

find_package(PCL REQUIRED)

# 仅使用 /usr/local 安装的 Draco（若你不需要 Draco，可注释掉本段与 sender/receiver 相关目标）
find_path(DRACO_INCLUDE_DIR draco/compression/encode.h
  PATHS /usr/local/include
  NO_DEFAULT_PATH
)
find_library(DRACO_LIBRARY draco
  PATHS /usr/local/lib
  NO_DEFAULT_PATH
)

# 可选：fast_gicp（存在则启用加速 GICP/VGICP）
find_package(fast_gicp QUIET)

# 可选：Zstd（系统库）
find_library(ZSTD_LIB zstd)
if(ZSTD_LIB)
  message(STATUS "Found zstd: ${ZSTD_LIB}")
else()
  message(WARNING "libzstd not found; RAW mode only (ignored if using Draco)")
endif()

# 生成 dynamic_reconfigure 代码（滑块）
generate_dynamic_reconfigure_options(
  cfg/Extrinsic.cfg
)

catkin_package(
  CATKIN_DEPENDS roscpp sensor_msgs pcl_ros pcl_conversions message_filters tf2_ros tf2_eigen dynamic_reconfigure
)

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
)
add_definitions(${PCL_DEFINITIONS})

# ------------------------
# 可执行文件
# ------------------------

# ---- 发送端（Draco）----
if(DRACO_INCLUDE_DIR AND DRACO_LIBRARY)
  include_directories(${DRACO_INCLUDE_DIR})
  add_executable(pc_udp_sender src/pc_udp_sender.cpp)
  target_link_libraries(pc_udp_sender
    ${catkin_LIBRARIES}
    ${DRACO_LIBRARY}
    pthread
  )
  if(ZSTD_LIB)
    target_compile_definitions(pc_udp_sender PRIVATE HAS_ZSTD=1)
    target_link_libraries(pc_udp_sender ${ZSTD_LIB})
  endif()
  # 确保运行时能在 /usr/local/lib 找到 libdraco.so
  set_target_properties(pc_udp_sender PROPERTIES
    BUILD_RPATH "/usr/local/lib"
    INSTALL_RPATH "/usr/local/lib"
  )

  # ---- 接收端（Draco）----
  add_executable(pc_udp_receiver src/pc_udp_receiver.cpp)
  target_link_libraries(pc_udp_receiver
    ${catkin_LIBRARIES}
    ${DRACO_LIBRARY}
    pthread
  )
  if(ZSTD_LIB)
    target_compile_definitions(pc_udp_receiver PRIVATE HAS_ZSTD=1)
    target_link_libraries(pc_udp_receiver ${ZSTD_LIB})
  endif()
  set_target_properties(pc_udp_receiver PROPERTIES
    BUILD_RPATH "/usr/local/lib"
    INSTALL_RPATH "/usr/local/lib"
  )

  message(STATUS "Using Draco include: ${DRACO_INCLUDE_DIR}")
  message(STATUS "Using Draco library: ${DRACO_LIBRARY}")
else()
  message(WARNING "Draco not found in /usr/local — pc_udp_sender/receiver will not be built.")
endif()

# ---- 多源拼接（带动态滑块/TF）----
add_executable(pc_multi_merger src/pc_multi_merger.cpp)
target_link_libraries(pc_multi_merger
  ${catkin_LIBRARIES}
  ${PCL_LIBRARIES}
)
add_dependencies(pc_multi_merger ${PROJECT_NAME}_gencfg)
if(fast_gicp_FOUND)
  target_compile_definitions(pc_multi_merger PRIVATE HAS_FAST_GICP=1)
  target_link_libraries(pc_multi_merger fast_gicp)
endif()

# ---- 纯 TF 拼接（极简；如果你有独立实现，保持这个目标，否则可移除）----
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/pc_tf_merger.cpp")
  add_executable(pc_tf_merger src/pc_tf_merger.cpp)
  target_link_libraries(pc_tf_merger ${catkin_LIBRARIES})
endif()

